//
// REPhotoThumbnailView.m
// REPhotoCollectionController
//
// Copyright (c) 2012 Roman Efimov (https://github.com/romaonthego)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

#import "REPhotoThumbnailView.h"
#import "REPhotoThumbnailsCell.h"
#import "Config.h"
#import "PictureManager.h"

@implementation REPhotoThumbnailView

@synthesize borderColor = _borderColor;

- (void)setPhoto:(Photo *)photo
{
//    [photoImageView sd_setImageWithURL:[NSURL URLWithString:photo.photoUrl]
//      forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"default_photo"]];
    if (photo.thumbnailImage == nil) {
        NSString *thumbnail=[PictureManager revertQNThumbnailUrl:photo.photoUrl];
        
        [photoImageView sd_setImageWithURL:[NSURL URLWithString:thumbnail] placeholderImage:[UIImage imageNamed:@"default_photo"]];
    }else{
        [photoImageView setImage:photo.thumbnailImage];
    }
   
    
    self.selected = photo.inMutilpleSelected;
    
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];

        photoImageView = [[UIImageView alloc]initWithFrame:self.bounds];
        [self addSubview:photoImageView];
        photoImageView.contentMode = UIViewContentModeScaleAspectFill;
        photoImageView.clipsToBounds=YES;
        
        whiteMaskView = [[UIView alloc]initWithFrame:self.bounds];
        whiteMaskView.backgroundColor=RGBA(255, 255, 255, 0.7);
        [self addSubview:whiteMaskView];
        
        selectedIndicator  = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width - 20, 2, 18, 18)];
        [self addSubview:selectedIndicator];
        selectedIndicator.image=[UIImage imageNamed:@"对号-选中"];
        
        imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        imageButton.frame = photoImageView.bounds;
        [self addSubview:imageButton];
        [imageButton addTarget:self action:@selector(tapImageBtn:) forControlEvents:UIControlEventTouchUpInside];


        
        
    }
    return self;
}

-(void)setSelected:(BOOL)aselected{
    _selected = aselected;
    selectedIndicator.hidden=_selected?NO:YES;
    whiteMaskView.hidden=selectedIndicator.hidden;

}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{    
    selectedIndicator.hidden=_selected?NO:YES;
}


-(void)tapImageBtn:(UIButton *)btn{
    NSLog(@"%ld", (long)btn.superview.tag);
    
    REPhotoThumbnailsCell *cell = (REPhotoThumbnailsCell *)self.parent;
    if ([cell isKindOfClass:[REPhotoThumbnailsCell class]] && cell.deleagte) {
        [cell.deleagte  didSelected:cell.indexPath.section thumbnail:(btn.superview.tag-1000) +cell.indexPath.row*4 thumbImgView:photoImageView];
    }
    
    
}


@end
